/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import database.DBPenjualan;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Penjualan;

/**
 *
 * @author elfatah
 */
public class PenjualanServlet extends HttpServlet {

    String pegawai, barang, id;
    String totharga, jmlbarang, action;
    String tanggal;
    Penjualan bean = new Penjualan();
    DBPenjualan dbobject = new DBPenjualan();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlv et request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // processRequest(request, response);
        action = request.getParameter("action");
        id = request.getParameter("id");
        if (action.equals("edit")) {
            try {
                //get userdetails of particular name
                Penjualan ubean = dbobject.getPenjualanDetails(id);
                request.setAttribute("updateuser", ubean);
                RequestDispatcher rd = request.getRequestDispatcher("insert_penjualan");
                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (action.equals("delete")) {
            try {
                //delete userdetails of particular name
                dbobject.deletePenjualan(id);
       
                RequestDispatcher rd = request.getRequestDispatcher("view_penjualan");
                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();

        pegawai = request.getParameter("pegawai");
        barang = request.getParameter("barang");
        tanggal = request.getParameter("tanggal");
        jmlbarang = request.getParameter("jmlbarang");
        totharga = request.getParameter("totharga");
        action = request.getParameter("action");
        id = request.getParameter("id");
        if (action.equals("Insert")) {
            try {
                setValuesToBean();

                dbobject.insertPenjualan(bean);
                out.print("<script>window.alert('Penambahan penjualan sukses');window.location=('insert_penjualan')</script>");

//                RequestDispatcher rd = request.getRequestDispatcher("insert_penjualan.jsp");
//                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PenjualanServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PenjualanServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        if (action.equals("Update")) {
            try {
                setValuesToBean();

                //using DBClass object call insertDetails method and pass bean object
                dbobject.UpdatePenjualan(bean, id);
          
                out.print("<script>window.alert('Perubahan data penjualan sukses');window.location=('view_penjualan')</script>");


            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public void setValuesToBean() {
        bean.setIdPegawai(pegawai);
        bean.setIdBarang(barang);
        bean.setJumlahBarang(jmlbarang);
        bean.setJumlahHarga(totharga);
        bean.setTgl(tanggal);

    }
}
